package Problem19;

/**
 * 这个题运用快慢指针来做，是快指针(fastNode)与慢指针(slowNode)路程差的问题
 * 设置快指针与慢指针中间相隔 n 个单位，当快指针 == null 时候，慢指针的下一个就是要删除的指针
 * 慢指针 slowNode.next = slowNode.next.next; 跳过下一个节点，即删除
 */
public class Solution {
    public static ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode headNode = new ListNode(9527);
        headNode.next = head;
        ListNode fastNode = headNode;
        ListNode slowNode = headNode;
        for ( n = n ; fastNode.next != null ; n--){
            if(n <= 0) {
                slowNode = slowNode.next;
            }
            fastNode = fastNode.next;
        }
        if(slowNode.next != null)
            slowNode.next = slowNode.next.next;
        return headNode.next;
    }
}

class ListNode {
    int val;
    ListNode next;
    ListNode(int x) {
        val = x;
    }
}